package com.example.controller;

import cn.hutool.core.io.FileUtil;
import com.example.common.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

@RestController
@RequestMapping("/files")
public class FileController {

    private static final String ROOT_PATH = System.getProperty("user.dir") + "/files";
    @Value("${ip}")
    private String ip;
    @Value("${server.port}")
    private String port;

    /*
     * 文件上传
     * */
    @PostMapping("/upload")
    public Result Upload(MultipartFile file) throws IOException {//接收前端传输
        String originalFilename = file.getOriginalFilename();//获取原始的文件名
        long flag = System.currentTimeMillis();//唯一标识处理重名
        String fileName = flag + "_" + originalFilename;
        File finalFile = new File(ROOT_PATH + "/" + fileName);//最终存到磁盘的文件对象
        if (!finalFile.getParentFile().exists()) {//若父级目录不存在则创建
            finalFile.getParentFile().mkdirs();
        }
        file.transferTo(finalFile);
        //后端生成并返回文件的url
        String url = "http://" + ip + ":" + port + "/files/download?fileName=" + fileName;
        return Result.success(url);
    }

    /*
     * 文件下载
     * */
    @GetMapping("/download")
    public void download(String fileName, HttpServletResponse response) throws IOException {
        File file = new File(ROOT_PATH + "/" + fileName);//文件在存盘存储的对象
        ServletOutputStream os = response.getOutputStream();
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setContentType("application/octet-stream");
//        os.write(FileUtil.readBytes(file));
        FileUtil.writeToStream(file,os);
        os.flush();
        os.close();
    }
}
